home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / utils / stlogin4.lzh / PWD.C < prev    next >
C/C++ Source or Header  |  1993-10-03  |  2KB  |  88 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <pwd.h>
  5.  
  6. FILE *pwd_fd;
  7. char pwd_line[250];
  8.  
  9. struct passwd *parse(char *p)
  10. {    int x;
  11.     static struct passwd pwent;
  12.     static char pwd_copy[250];
  13.     char *entry[NRE];
  14.  
  15.     p=memcpy(pwd_copy,p,249);
  16.     pwd_copy[249]=0;    /* don't damage original line */
  17.  
  18.     for(x=0;x<NRE;x++)
  19.     {    entry[x]=p;
  20.         if ((p=strchr(p,':')) != NULL)
  21.             *p++='\0';
  22.     }
  23.     pwent.pw_name  =entry[NAME];    pwent.pw_dir =entry[HOME];
  24.     pwent.pw_shell =entry[SHELL];    pwent.pw_uid =atoi(entry[UID]);
  25.     pwent.pw_passwd=entry[PWD];    pwent.pw_gid =atoi(entry[GID]);
  26.     return &pwent;
  27. }
  28.  
  29. void setpwent (void)
  30. {    if (pwd_fd != NULL)    fseek(pwd_fd,0,SEEK_SET);
  31.     else pwd_fd = fopen(PASSWD, "r");
  32.  
  33.     if(pwd_fd == NULL)    /* last chance ! */
  34.         if((pwd_fd=fopen("c:\\etc\\passwd","r")) == NULL) 
  35.             fputs("pwd: Can't open PASSWD file !!\n",stderr);
  36. }
  37.  
  38. struct passwd *getpwent (void)
  39. {
  40.     if (pwd_fd == NULL)    /* file not open yet */
  41.         setpwent();
  42.     if (fgets(pwd_line,(int)sizeof(pwd_line)-1,pwd_fd) == NULL)
  43.         return NULL;
  44.  
  45.     return parse(pwd_line);
  46. }
  47.  
  48. void endpwent (void)
  49. {    if (pwd_fd != NULL)
  50.     {    fclose (pwd_fd);
  51.         pwd_fd = NULL;
  52.     }
  53. }
  54.  
  55. struct passwd *getpwnam(name)
  56. char *name;
  57. {    struct passwd *pwent;
  58.  
  59.     setpwent();
  60.     while(1)
  61.     {    if((pwent=getpwent()) == NULL)
  62.         {    endpwent();
  63.             return NULL;    /* loginname not found */
  64.         }
  65.         if(! strncmp(name,pwent->pw_name,strlen(pwent->pw_name)))
  66.             break;  /* login name found */
  67.     }
  68.     endpwent();
  69.     return pwent;
  70. }
  71.  
  72. struct passwd *getpwuid(uid)
  73. int uid;
  74. {    struct passwd *pwent;
  75.  
  76.     setpwent();
  77.     while(1)
  78.     {    if((pwent=getpwent()) == NULL)
  79.         {    endpwent();
  80.             return NULL;    /* loginname not found */
  81.         }
  82.         if(pwent->pw_uid == uid)
  83.             break;  /* user id found */
  84.     }
  85.     endpwent();
  86.     return pwent;
  87. }
  88.